package com.whh.dota2helper.service;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.whh.dota2helper.Constant;
import com.whh.dota2helper.model.Ability;
import com.whh.dota2helper.self.AbstractDBHelper;

public class AbilityService extends AbstractDBHelper{
	private Context context;
	
	public AbilityService(Context context){
		this.context = context;
	}
	
	public Ability getAbility(long id) {
		super.open(context);
		Ability ability = null;
		String selection = "id="+id;
		Cursor cursor = db.query(getTableName(), null, selection, null,null, null, null);
		cursor.moveToFirst();
		if (!cursor.isAfterLast() && (cursor.getString(0) != null)) {
			ability = new Ability();
			ability.setId(cursor.getLong(0));
			ability.setHeroId(cursor.getLong(1));
			ability.setName(cursor.getString(2));
			ability.setImg(cursor.getString(3));
			ability.setDesc(cursor.getString(4));
			ability.setManaCost(cursor.getString(5));
			ability.setCoolDown(cursor.getString(6));
			ability.setEffect(cursor.getString(7));
			ability.setTips(cursor.getString(8));
		}
		cursor.close();
		super.close();
		Log.i(Constant.LOG_TAG,"ability.getAbility:"+ability.getName());
		return ability;
	}

	public boolean save(Ability ability) {
		super.open(context);
		ContentValues values = new ContentValues();
		values.put("hero_id", ability.getHeroId());
		values.put("name", ability.getName());
		values.put("img", ability.getImg());
		values.put("desc", ability.getDesc());
		values.put("mana_cost", ability.getManaCost());
		values.put("cool_down", ability.getCoolDown());
		values.put("effect", ability.getEffect());
		values.put("tips", ability.getTips());
		Long uid = db.insert(getTableName(), "id", values);
		Log.i(Constant.LOG_TAG, "ability.save:"+uid);
		super.close();
		return uid>0;
	}


	public List<Ability> getAbilities(long heroId) {
		super.open(context);
		String selection = "hero_id="+heroId;
		List<Ability> list = new ArrayList<Ability>();
		Cursor cursor = db.query(getTableName(), null, selection, null,null, null, "id ASC");
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && (cursor.getString(0) != null)) {
			Ability ability = new Ability();
			ability.setId(cursor.getLong(0));
			ability.setHeroId(cursor.getLong(1));
			ability.setName(cursor.getString(2));
			ability.setImg(cursor.getString(3));
			ability.setDesc(cursor.getString(4));
			ability.setManaCost(cursor.getString(5));
			ability.setCoolDown(cursor.getString(6));
			ability.setEffect(cursor.getString(7));
			ability.setTips(cursor.getString(8));
			list.add(ability);
			cursor.moveToNext();
		}
		cursor.close();
		super.close();
		Log.i(Constant.LOG_TAG,"ability.getAbilities:"+list.size());
		return list;
	}
	
	public static String getTableName() {
		return "ability";
	}

	public static String getCreateTableSQL() {
		return "CREATE TABLE IF NOT EXISTS " + getTableName() + "("+
				"id INTEGER PRIMARY KEY, " +
				"hero_id INTEGER, " +
				"name VARCHAR(50), " +
				"img VARCHAR(50)," +
				"desc VARCHAR(500)," +
				"mana_cost VARCHAR(50)," +
				"cool_down VARCHAR(50)," +
				"effect VARCHAR(1000),"+
				"tips VARCHAR(1000)"+
				")";
	}

}
